<?php
namespace app\home\controller;
use think\Controller;
use think\Request;
use think\Db;

/**
 * @title 登陆注册
 * @group 樱桃荟
 */
class Login extends Controller
{
    public $time = 300;                             // 验证码过期时间
    /**
     * @title 注册
     * @url /home/Login/register
     * @method POST
     * @param name:phone type:int require:1 other: desc:手机号
     * @param name:pCode type:int require:1 other: desc:手机验证码
     * @param name:password type:string require:1 other: desc:密码
     * @param name:rpass type:string require:1 other: desc:确认密码
     * @param name:reCode type:int require:1 other: desc:邀请码
     */
    public function register(Request $request)
    {
        $phone      = $request->param('phone','','trim');
        $pCode      = $request->param('pCode','','trim');
        $password   = $request->param('password','','trim');
        $rpass      = $request->param('rpass','','trim');
        $reCode     = $request->param('reCode','','trim');
        if (!$phone || !$pCode || !$password || !$rpass || !$reCode) 
        {
            return json_error('请填写所有数据');
        }
        if($password != $rpass) return json_error('两次密码不一致');
        if(strlen($password) < 6) return json_error('密码不能小于六位');
        $cInfo  = Db::name('phone_code')->where('user_phone',$phone)->order('mes_id desc')->find();
        if(!$cInfo) return json_error('验证码错误'); 
        $cArr   = explode(',', $cInfo['phone_code']);
        $dbCode = substr($cArr[0], 0, -2);
        $exTime = $cArr[1]+$this->time;
        if(!$cInfo || $dbCode!=$pCode || $exTime < time()) return json_error('验证码不正确或已过期');
        $uInfo  = Db::name('user')->where('user_phone',$phone)->find();
        if($uInfo) return json_error('该手机号已被注册');
        $parent = Db::name('user')->where('user_reg_code',$reCode)->find();
        if(!$parent) return json_error('邀请码错误');
        $reCode = $this->getInvitationCode();
        $uArr['user_phone']     = $phone;
        $uArr['user_reg_code']  = $reCode;
        $uArr['user_password']  = md5($password);
        $uArr['user_parent']    = $parent['user_id'];
        $uArr['create_time']    = date("Y-m-d H:i:s");
        $uArr['level_path']     = $parent['level_path'].','.$parent['user_id'];
        $rN = Db::name('user')->insert($uArr);
        if($rN)
        {
            return json_success('注册成功');
        }
        else
        {
            return json_error('注册失败');
        }
    }

    /**
     * @title 登录接口
     * @url /home/Login/login
     * @method POST
     * @param name:phone type:int require:1 other: desc:手机号
     * @param name:password type:string require:1 other: desc:密码
     * @return token:token
     *
     */
    public function login(Request $request)
    {
        $phone  = $request->param('phone');
        $pass   = $request->param('password');
        if(!$phone || !$pass) return json_error('请填写手机号或密码');
        $uInfo  = Db::name('user')->where('user_phone',$phone)->find();
        if(!$uInfo || $uInfo['is_delete'] ==1) return json_error('账号不存在或已删除'); 
        if(md5($pass) != $uInfo['user_password']) return json_error('密码错误');
        if(!$uInfo['user_status'])
        {
            $cInfo  = Db::name('config')->find();
            if(!$cInfo) return json_error('登录失败1');
            $udArr  = ['user_gold' => $cInfo['register_give'], 'user_status' => 1];
        } 
        $token  = $this->getToken($uInfo['user_id']);
        $udArr['user_token']  = $token;
        $uNnn   = Db::name('user')->where('user_id',$uInfo['user_id'])->data($udArr)->update();
        $sN     = Db::name('user')->where('user_token',$token)->find();
        if($sN)
        {
            return json_success(['token' => $token],'登录成功');
        }
        else
        {
            return json_error('登录失败2');
        }
    }

    /**
     * @title 忘记密码
     * @url /home/Login/resetPass
     * @method POST
     * @param name:phone type:int require:1 other: desc:手机号
     * @param name:pCode type:int require:1 other: desc:手机验证码
     * @param name:password type:string require:1 other: desc:密码
     * @param name:repass type:string require:1 other: desc:确认密码
     * @return token:token
     *
     */
    public function resetPass(Request $request)
    {
        $phone      = $request->param('phone','','trim');
        $pCode      = $request->param('pCode','','trim');
        $password   = $request->param('password','','trim');
        $rpass      = $request->param('rpass','','trim');
        if (!$phone || !$pCode || !$password || !$rpass) 
        {
            return json_error('请填写所有数据');
        }
        if($password != $rpass) return json_error('两次密码不一致');
        if(strlen($password) < 6) return json_error('密码不能小于六位');
        $cInfo  = Db::name('phone_code')->where('user_phone',$phone)->order('mes_id desc')->find();
        if(!$cInfo) return json_error('验证码错误'); 
        $cArr   = explode(',', $cInfo['phone_code']);
        $dbCode = substr($cArr[0], 0, -2);
        $exTime = $cArr[1]+$this->time;
        if(!$cInfo || $dbCode!=$pCode || $exTime < time()) return json_error('验证码不正确或已过期');
        $user = Db::name('user')->where('user_phone',$phone)->find();
        if(md5($password) == $user['user_password']) return json_error('新旧密码不能相同');
        $udN = Db::name('user')->where('user_phone',$phone)->update(['user_password' => md5($password)]);
        if($udN) 
        {
            return json_success('修改成功');
        }
        else
        {
            return json_error('修改失败');
        }
    }

    /**
     * @title 获取手机验证码
     * @url /home/Login/getcode
     * @method POST
     * @param name:phone type:int require:1 default:1 other: desc:手机号
     *
     */
    public function getcode(Request $request)
    {
        $phone  = $request->param('phone');
        return sendPhoneCode($phone);
    }

    // 生成邀请码
    public function getInvitationCode()
    {
        $iCode = mt_rand(111111,999999);
        $cN = Db::name('user')->where('user_reg_code',$iCode)->find();
        if($cN) $this->getInvitationCode();
        return $iCode;
    }

    // 生成token
    public function getToken($uid)
    {
        $token  = $uid.'.'.md5(mt_rand(1000000,9999999).time());
        $tN     = Db::name('user')->where('user_token',$token)->find();
        if($tN) $this->getToken($uid);
        $upN    = Db::name('user')->where('user_id',$uid)->update(['user_token' => $token]);
        if($upN) return $token;
    }
}